/*
 * restserver - A smart, efficient, low consumption RESTful application service platform writen in C
 * author	: calvin
 * email	: calvinwilliams@163.com
 *
 * Licensed under the Apache License v2.0, see the file LICENSE in base directory.
 */

#include "restserver_api.h"
#include "FormDataParser_c_api.h"
#include <time.h>

funcRestServiceEntry StreamChannel;
funcRestServiceEntry send123;

static struct RestServiceConfig		g_rest_services_config[] = {
		{ "GET" , "/test/test" , StreamChannel } ,
		{ "POST" , "/send_picture" , send123} ,
		{ "" , "" , NULL }
	} ;

int StreamChannel( struct RestServerContext *ctx )
{
	char		response[1024] ;
	int		response_len ;
	char*       method = NULL ;
	int		      method_len = 0 ;
	int		nret = 0 ;

	memset(response , 0x00 , sizeof(response) );
	response_len = 0 ;

	method = RSAPIGetHttpMethodPtr( ctx , & method_len ) ;
    printf("recieve request:\n%s\n\n", method);

	
	memset( response , 0x00 , sizeof(response) );
	response_len = 0 ;
	
	response_len = snprintf( response , sizeof(response) , "Hello restserver  123456\n" ) ;
	
	nret = RSAPIFormatHttpResponse( ctx , response , response_len , NULL ) ;
	if( nret )
		return nret;
	
	return 0;
}

int send123( struct RestServerContext *ctx )
{
	char		response[1024] ;
	int		response_len ;
	char*       method = NULL ;
	int		      method_len = 0 ;
	char*       pReqBody = NULL;
	int         iReqDataLen = 0;
	int		nret = 0 ;
	int     i = 0;
	
	memset(response , 0x00 , sizeof(response) );
	response_len = 0 ;

	pReqBody = RSAPIGetHttpRequestBodyPtr( ctx , &iReqDataLen);//获取请求body
	//printf("recieve request:\n%s\n\n", pReqBody);

	struct tm *timep;
    struct timespec start_spec;
    clock_gettime(0, &start_spec);
    timep = localtime(&start_spec.tv_sec);
    printf("%d-%d-%d %d:%d %d\n", 1900 + timep->tm_year, 1 + timep->tm_mon, timep->tm_mday,
        timep->tm_hour, timep->tm_min, timep->tm_sec);

	printf("*********************************\n");
	method = RSAPIGetHttpMethodPtr( ctx , & method_len ) ;
    printf("recieve request:\n%s\n\n", method);
	printf("*********************************\n");

	//FILE *pf = fopen("./formdata.bin", "wb");
	//fwrite(pReqBody, iReqDataLen, 1, pf);
	//fclose(pf);

	int num = 0;
    FormNoda* formNodas = parser_form_data(pReqBody, iReqDataLen, &num);

	for(i = 0; i < num; i++)
    {
        if(formNodas[i].isFile)
		{
			//如果noda是文件, 则将文件保存在本地
			printf("%s:%s\n", formNodas[i].mName, formNodas[i].mFileName);
			FILE *pf = fopen(formNodas[i].mFileName, "wb");
			fwrite(formNodas[i].mContent, formNodas[i].mContentLen, 1, pf);
			fclose(pf);
		}
		else
		{
			//如果noda不是文件, 则打印出name和content
			printf("%s:%s\n", formNodas[i].mName, formNodas[i].mContent);
		}
    }
    free_form_data(formNodas, num);//回收formNodas空间
	

	//构造返回值
	memset( response , 0x00 , sizeof(response) );
	response_len = 0 ;
	
	response_len = snprintf( response , sizeof(response) , "Hello restserver  send picture\n" ) ;
	
	nret = RSAPIFormatHttpResponse( ctx , response , response_len , NULL ) ;
	if( nret )
		return nret;
	
	return 0;
}

funcInitRestApplication InitRestApplication;
int InitRestApplication( struct RestServerContext *ctx )
{
	struct RestServiceControler	*ctl = NULL ;
	
	/* ����RESTful��������� */
	ctl = RSAPICreateRestServiceControler( g_rest_services_config ) ;
	if( ctl == NULL )
		return RESTSERVER_FATAL_CREATE_RESTSERVICECONTROLER;
	
	/* ����RESTful�����������restserverƽ̨�����Ļ����� */
	RSAPISetUserData( ctx , ctl );
	
	return 0;
}

#include "LOGC.h"

funcCallRestApplication CallRestApplication;
int CallRestApplication( struct RestServerContext *ctx )
{
	struct RestServiceControler	*ctl = NULL ;
	
	int				nret = 0 ;
	
	/* ��restserverƽ̨�����Ļ�����ȡ��RESTful��������� */
	ctl = RSAPIGetUserData( ctx ) ;
	if( ctl == NULL )
		return RESTSERVER_FATAL_GET_RESTSERVICECONTROLER;
	
	/* ��RESTful������������ɷ�������� */
	nret = RSAPIDispatchRestServiceControler( ctl , ctx ) ;
	if( nret )
		return nret;
	
	return 0;
}

funcCleanRestApplication CleanRestApplication;
int CleanRestApplication( struct RestServerContext *ctx )
{
	struct RestServiceControler	*ctl = NULL ;
	
	/* ��restserverƽ̨�����Ļ�����ȡ��RESTful��������� */
	ctl = RSAPIGetUserData( ctx ) ;
	
	/* ����RESTful��������� */
	RSAPIDestroyRestServiceControler( ctl );
	
	return 0;
}

